package de.teamgamma.cansat.groundstation.livestream;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONObject;

/**
 * Container that logs data transmitted on the livestream
 * @author Marc Huisinga
 *
 */
public class LivestreamData {
	/**
	 * Default for the property that defines that the data should get deleted after FRAMESIZE is exceeded
	 */
	public static final boolean LIMITEDFRAME = false;
	/**
	 * Default size of LivestreamData if the data is supposed to get limited
	 */
	public static final int FRAMESIZE = 20;
	
	private HashMap<String,ArrayList<String>> data;
	private boolean limitedFrame;
	private int frameSize;
	
	public LivestreamData() {
		data = new HashMap<String,ArrayList<String>>();
		limitedFrame = LIMITEDFRAME;
		frameSize = FRAMESIZE;
	}
	
	/**
	 * Updates the LivestreamData with a json object
	 * @param json - JSONObject to update with
	 */
	public void updateDataWithJson(JSONObject json) {
		for (String key : TransmittedData.ALLKEYS) {
			if (TransmittedData.STRINGKEYS.contains(key)) {
				addDataToKey(key, json.getString(key));
			} else {
				double value = json.getDouble(key);
				addDataToKey(key, String.valueOf(value));
			}
		}
	}
	
	/**
	 * Adds a data value to the LivestreamData
	 * @param key - key to be added to
	 * @param dataValue - value to be added
	 */
	public void addDataToKey(String key, String dataValue) {
		if (data.containsKey(key)) {
			if (limitedFrame && data.get(key).size() == frameSize) {
				data.remove(data.get(key).remove(0));
			}
			data.get(key).add(dataValue);
		} else {
			data.put(key, new ArrayList<String>());
			data.get(key).add(dataValue);
		}
	}
	
	/**
	 * Gets a specified date
	 * @param key - key of the date
	 * @param index - index in the list of data
	 * @return date
	 */
	public String getDate(String key, int index) {
		if (data.containsKey(key)) {
			return data.get(key).get(index);
		} else {
			return null;
		}
		
	}
	
	/**
	 * Gets the entire data of a key
	 * @param key
	 * @return data
	 */
	public String[] getDataOfKey(String key) {
		String[] dataOfKey = null;
		if (data.containsKey(key)) {
			dataOfKey = new String[data.get(key).size()];
			int dataIndex = 0;
			for (String currentDate : data.get(key)) {
				dataOfKey[dataIndex] = currentDate;
				dataIndex ++;
			}
			
		}
		return dataOfKey;
	}

	/**
	 * Returns if the size of the data is limited
	 * @return size of the data is limited
	 */
	public boolean isLimitedFrame() {
		return limitedFrame;
	}

	/**
	 * Sets if the size of the data is limited
	 * @param limitedFrame
	 */
	public void setLimitedFrame(boolean limitedFrame) {
		this.limitedFrame = limitedFrame;
	}

	/**
	 * Gets the allowed maximal size of the data
	 * @return size
	 */
	public int getFrameSize() {
		return frameSize;
	}

	/**
	 * Sets the allowed maximal size of the data
	 * @param frameSize - allowed maximal size of the data
	 */
	public void setFrameSize(int frameSize) {
		this.frameSize = frameSize;
	}
}
